# -*- coding: utf-8 -*-
"""
@author:xieyabin
@file: ceshi.py
@time: 2019/01/17
"""
# import pymysql

# a = ['10', '000', '000']
# b = ''
# for i in a:
#     b += i
# print(b)
import re

import time

import redis
from pymysql import *

def get():
    conn = connect(  # host='localhost',
        host='123.160.246.41',
        port=3366,
        database='bx_abc',
        # user='mha_user',
        user='caiji_user',
        # password='xyb.1206',
        # password='gc895316',
        password='bxgj2019',
        charset="utf8",
    )
    cs = conn.cursor()
    # addtime = int(time.time())
    # LIMIT = 2
    # i = 0
    # while True:
    #     sql = 'select distinct keyword from keyword_search limit {},{}'.format( i * LIMIT, LIMIT)
    #     rt = cs.execute(sql)
    #     rt_list = cs.fetchall()
    #     print(rt_list)
    #     for a in rt_list:
    #         print(a[0]+' www.sohu.com/a/')
    #     if rt < LIMIT:
    #         cs.close()
    #         break
    #     i += 1
    sql = 'select kw from init_kw '
    sql2 = 'insert ignore into keyword_search (keyword,relatedwords,parent_id,level,results_num,addtime,from_source) values(%s,%s,%s,%s,%s,%s,%s)'
    rt = cs.execute(sql)
    rt_list = cs.fetchone()
    # for index,i in enumerate(rt_list):
    #     # count1 = cs.execute(sql2, (i[0], i[0], 0, 0, 0, addtime, 0))
    print(rt_list)

    conn.commit()
    cs.close()
    conn.close()
    red = redis.StrictRedis(host='123.160.246.41',
                                 port=6379,
                                 db=11)
    print(red)
if __name__ == '__main__':
    # get()
    # a = ['a','b']
    # a = '\n'.join(a)
    # print(a)
    # a = "URL='http://www.sohu.com/a/281562898_104952'>"
    # f = re.search(r"URL='(.*)'", a).group(1)
    # print(f)
    # b = 'https://weixin.sogou.com/weixin?query=保险&type=2'
    # f = re.search(r"=(.*)&",b).group(1)
    # print(f)
    # kw = []
    # conn = connect(  # host='localhost',
    #     host='192.168.3.191',
    #     port=3306,
    #     database='keyword_search',
    #     user='root',
    #     # password='xyb.1206',
    #     password='abcd@1234',
    #     charset="utf8",
    # )
    # cs = conn.cursor()
    # sql = 'select kw from init_kw where id=7797'
    # rt = cs.execute(sql)
    # rt_list = cs.fetchall()
    # for i in rt_list:
    #     print(i)
    #     sql = "select id from keyword_search where relatedwords='{}'".format(i[0])
    #     rt1 = cs.execute(sql)
    #     rt_list1 = cs.fetchone()
    #     if not rt_list1:
    #         kw.append(i[0])
    #     print(kw)
    # conn = connect(  # host='localhost',
    #     host='123.160.246.41',
    #     port=3366,
    #     database='bx_abc',
    #     user='caiji_user',
    #     # password='xyb.1206',
    #     password='bxgj2019',
    #     charset="utf8",
    # )
    # err_url = 'https://www.baidu.com/s?wd=02195511%E9%AA%97%E5%B1%80%E8%BF%87%E7%A8%8B%20%20%20%20www.sohu.com/a/'
    # cs = conn.cursor()
    # sql = 'insert ignore into bx_caiji_errurl (err_url) values("{}")'.format(err_url)
    # print(sql)
    # rt = cs.execute(sql)
    # conn.commit()
    # cs.close()
    # conn.close()
    conn = connect(  # host='localhost',
        host='218.28.2.162',
        port=3306,
        database='bx_caiji',
        user='caiji_user',
        # password='xyb.1206',
        password='bxgj2019',
        charset="utf8",
    )
    cs = conn.cursor()
    while 1:
        time.sleep(5)
        sql = "DELETE from   bx_caiji_keywords WHERE id > 6000000 and keyword not REGEXP '保|险|疾|理|寿|伤|意|康|太|平|华夏|赔'"
        rt = cs.execute(sql)
        print(rt)
        conn.commit()
